//
//  UserLogin.swift
//  BookStore
//
//  Created by JinYuXin on 2022/5/9.
//


import Foundation
import UIKit

class UserLoginViewController : UIViewController{
    var user:String!
    let defaults = UserDefaults.standard
    var txtUser:UITextField!
    var txtpwd:UITextField!
    var btnLogin:UIButton!
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        let mainSize = UIScreen.main.bounds.size
        
        let vLogin = UIView(frame:CGRect(x:30,y:200,width:mainSize.width-30,height:250))
        vLogin.layer.borderWidth = 0.5
        vLogin.layer.borderColor = UIColor.lightGray.cgColor
        vLogin.backgroundColor = UIColor.white
        self.view.addSubview(vLogin)
        
        txtUser = UITextField(frame: CGRect(x:30,y:30,width: vLogin.frame.size
            .width - 60, height: 44))
        txtUser.layer.cornerRadius = 5
        txtUser.layer.borderColor = UIColor.lightGray.cgColor
        txtUser.layer.borderWidth = 0.5
        txtUser.autocapitalizationType = .none
        txtUser.leftView = UIView(frame: CGRect(x:0,y:0,width: 44,height: 44))
        txtUser.leftViewMode = UITextField.ViewMode.always
        
        let imgUser = UIImageView(frame: CGRect(x: 11, y: 11, width: 22, height: 22))
        imgUser.image = UIImage(named: "iconfont-user")
        txtUser.leftView!.addSubview(imgUser)
        vLogin.addSubview(txtUser)
        
        txtpwd = UITextField(frame: CGRect(x: 30, y: 90, width: vLogin.frame.size.width - 60, height: 44))
        txtpwd.layer.cornerRadius = 5
        txtpwd.layer.borderColor = UIColor.lightGray.cgColor
        txtpwd.layer.borderWidth = 0.5
        txtpwd.isSecureTextEntry = true
        txtpwd.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 44, height: 44))
        txtpwd.leftViewMode = UITextField.ViewMode.always
        
        let imgPwd = UIImageView(frame: CGRect(x: 11, y: 11, width: 22, height: 22))
        imgPwd.image = UIImage(named: "iconfont-password")
        txtpwd.leftView!.addSubview(imgPwd)
        vLogin.addSubview(txtpwd)
        
        btnLogin = UIButton(frame: CGRect(x:mainSize.width/2-120/2,y:150,width: 120,height: 50))
        btnLogin.setTitle("登陆", for: .normal)
        btnLogin.backgroundColor = UIColor.gray
        vLogin.addSubview(btnLogin)
        
        if defaults.string(forKey: "userName") != nil{
            txtUser.text = defaults.string(forKey: "userName")
        }
        
        btnLogin.addTarget(self, action: #selector(loginEvent), for: .touchUpInside)
        
    }
    @objc func loginEvent(){
        let usercode = txtUser.text!
        let password = txtpwd.text!
        txtUser.resignFirstResponder()
        txtpwd.resignFirstResponder()
        let queryPwd = UserTable.GetPwd(txtUser.text!)
        let pwd = queryPwd!.description
        if pwd == "[]"{
            let p = UIAlertController(title: "登陆失败", message: "用户名或密码错误", preferredStyle: .alert)
            p.addAction(UIAlertAction(title: "确定", style: .default, handler: {(act:UIAlertAction) in self.txtpwd.text = "" }))
            present(p,animated: false, completion: nil)
        }else{
            if queryPwd![0]["pwd"] as! String==txtpwd.text!
            {
                defaults.set(txtUser.text!, forKey: "userName")
                user = txtUser.text!
                let mainBoard:UIStoryboard! = UIStoryboard(name: "Main", bundle: nil)
                let VCMain = mainBoard!.instantiateViewController(withIdentifier: "vcMain") as! MainViewController
                VCMain.user = user
                UIApplication.shared.windows[0].rootViewController = VCMain
            }
            else
            {
                let p = UIAlertController(title: "登陆失败", message: "用户名或密码错误", preferredStyle: .alert)
                p.addAction(UIAlertAction(title: "确定", style: .default, handler: {(act:UIAlertAction) in self.txtpwd.text = "" }))
                present(p,animated: false, completion: nil)
                
            }
            
        }
            
    }
}

